package cn.kmia.hrm.action.system;

import cn.kmia.hrm.pojo.system.User;
import cn.kmia.hrm.pojo.system.UserRole;
import cn.kmia.hrm.service.system.UserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * @program: hrm
 * @description: 用户角色对应操作
 * @author: 4K
 * @create: 2018-05-08 10:40
 **/
@IocBean
@At("/userRole")
public class UserRoleAction {

    private static final Log log = Logs.get();

    @Inject
    protected UserRoleService userRoleService;

    /**
     * 设置
     *
     * @param userRoles 用户角色信息
     * @param user    操作者
     * @return
     */
    @At("/edit")
    @Ok("json:full")
    @RequiresPermissions("userRole:edit")
    public Object edit(@Param("userRoles") UserRole[] userRoles, @Attr(scope = Scope.SESSION, value = "user") User user) {
        NutMap nm = new NutMap();
        try {
            userRoleService.edit(userRoles,user.getUser_idcard());
            nm.setv("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            nm.setv("success", false).setv("errorMsg", "设置用户角色失败，请联系管理员");
        }
        return nm;
    }

    /**
     * 删除
     *
     * @param userIds 多个用逗号分隔
     * @return
     */
    @At("/del")
    @Ok("json:full")
    @RequiresPermissions("userRole:edit")
    public Object del(@Param("userIds") String userIds) {
        NutMap re = new NutMap();
        try {
            userRoleService.delete(userIds);
            re.setv("success", true);
        } catch (Exception e) {
            re.setv("success", false);
            log.error("删除用户角色异常", e);
        }
        return re;
    }
}
